<!--
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2021-12-23 09:06:25
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-08-10 09:19:10
-->
<template>
  <div>
    <!-- <input v-model="inputName" />
    <parent></parent> -->
    <el-input v-model="money" v-addQwf="money" />
  </div>
</template>
<script>
import parent from "./parent.vue";
export default {
  name: "swRegister",
  components: {
    parent,
  },
  directives: {
    addQwf: {
      update(el, binding) {
        console.log("update el binding ", el, binding);
        let n = binding.value;
        if (!n) return n;
        let str = n.split(".");
        let re = /\d{1,3}(?=(\d{3})+$)/g;
        let n1 = str[0].replace(re, "$&,");
        el.getElementsByTagName("input").value = n1 + ".00";
        console.log("dlj n1  ", n1, el.getElementsByTagName("input"), binding);
        //return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}.00`;
      },
      bind(el, binding) {
        console.log("bind el binding ", el, binding);
        let n = binding.value;
        if (!n) return n;
        let str = n.split(".");
        let re = /\d{1,3}(?=(\d{3})+$)/g;
        let n1 = str[0].replace(re, "$&,");
        console.log("dlj n1  ", n1, document.getElementsByTagName("input"));
        el.getElementsByTagName("input")[0].value = n1 + ".00";
      },
    },
  },
  data() {
    return {
      tdata: [
        {
          id: "1",
          name: "dlj1",
        },
        {
          id: "2",
          name: "dlj2",
        },
        {
          id: "3",
          name: "dlj3",
        },
      ],
      inputName: "",
      money: "111111",
    };
  },
  provide() {
    return {
      tdt: this.tdata, //传递数据
      showMsg: this.showMsg, //传递方法
      showInput: () => this.inputName, //实现响应式
    };
  },
  methods: {
    showMsg() {
      console.log("祖父组件中定义的方法");
    },
    // 金额添加千分位
    comdify(n) {
      if (!n) return n;
      let str = n.split(".");
      let re = /\d{1,3}(?=(\d{3})+$)/g;
      let n1 = str[0].replace(re, "$&,");
      return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}.00`;
    },
    //去除千分位中的‘，'
    delcommafy(num) {
      if (!num) return num;
      num = num.toString();
      num = num.replace(/,/gi, "");
      return num;
    },
  },
};
</script>